<template>
  <div class="app-container">
    <span style="font-size: medium;color: rgba(151,170,195) "> / 采购订单 / 订单维护</span>

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="85px" style="margin-top: 20px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入采购订单编号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
              v-for="dict in purchase_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
        >删除采购订单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderMaintainList" @selection-change="handleSelectionChange"
              style="margin-top: 15px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" fixed align="center" prop="orderNo" width="125">
        <template #default="scope">
          <router-link :to="'/OrderDetail/index/' + scope.row.orderNo" class="link-type">
            <span>{{ scope.row.orderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="供货商名称" align="center" prop="companyName" width="120"/>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="120">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="responsiblePerson" width="100"/>
      <el-table-column label="修改人" align="center" prop="modifiedBy" width="100"/>
      <el-table-column label="修改日期" align="center" prop="modifiedDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.modifiedDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <el-tag type="primary" disable-transitions>
            {{ scope.row.orderStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" header-align="center" prop="remarks" width="180"/>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="EditPen" @click="handleChange(scope.row)" v-if="scope.row.orderStatus === '已确认'
            || scope.row.orderStatus === '到出口港'">变更</el-button>
          <el-button link type="primary" icon="Check" @click="handleConfirm(scope.row)" v-if="scope.row.orderStatus === '待确认'">确认订单</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-if="scope.row.orderStatus === '已入库'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup>
import useUserStore from '@/store/modules/user'
import {getSupplierByUsername} from "@/api/purchase/Supplier.js";
import {parseTime} from "@/utils/ruoyi.js";
import {getPurchaseOrder, listPurchaseOrder, updatePurchaseOrderStatus} from "@/api/purchase/purchaseOrder.js";
import {getChangeOrderDetail} from "@/api/purchase/orderDetail.js";
import {addSupplyDetailList} from "@/api/purchase/supplyDetail.js";

const { proxy } = getCurrentInstance();
const { purchase_order_status } = proxy.useDict('purchase_order_status');

const username = ref('');
const supplier = ref({});
const orderMaintainList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const supplyDetailList = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const orderInfo =ref({});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    companyName: null,
    orderStatus: null,
  },
  rules: {
    origin:[
        { required: true, message: "货源地不能为空", trigger: "blur" }
    ],
    productionDate:[
        { required: true, message: "生产日期不能为空", trigger: "blur" }
    ],
    shelfLife:[
        { required: true, message: "保质期不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

function initial(){
  username.value = useUserStore().name
  if(username.value !== undefined){
    getSupplierByUsername(username.value).then(response => {
      supplier.value = response.data;
      handleQuery();
    })
  }
}

/** 查询订单维护列表 */
function getList() {
  loading.value = true;
  listPurchaseOrder(queryParams.value).then(response => {
    orderMaintainList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderNo);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 查询按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  if(supplier.value === undefined){
    queryParams.value.companyName = 'null';
  }else{
    queryParams.value.companyName = supplier.value.companyName;
  }
  getList();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderNos = row.orderNo || ids.value;
  proxy.$modal.confirm('是否确认删除订单维护编号为"' + _orderNos + '"的数据项？').then(function() {
    return delPurchaseOrder(_orderNos);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('purchaseOrder/export', {
    ...queryParams.value
  }, `orderMaintain_${new Date().getTime()}.xlsx`)
}

/** 订单确认 **/
function handleConfirm(row){
  const _orderNo = row.orderNo;
  proxy.$modal.confirm('是否确认当前订单？').then(function() {
    getPurchaseOrder(_orderNo).then(response => {
      orderInfo.value = response.data;
      updateToPickUp();
    });
  })
}

/** 订单变更 **/
function handleChange(row){
  proxy.$router.push({
    path:'/Provider/ChangeDetail',
    query:{
      orderNo: row.orderNo
    }
  });
}

/** 确认订单后状态更新 */
function updateToPickUp(){
  orderInfo.value.orderStatus = '已确认';
  updatePurchaseOrderStatus(orderInfo.value).then(response => {
    proxy.$modal.msgSuccess("状态更新成功");
    initial();
  })
}

initial();
</script>

<style scoped lang="scss">

</style>